/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package Tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;

/**
 *
 * @author Иван
 */
public class CommandExtraThreadIOStream implements Runnable,IExtraThreadFactory {

    public Thread t;
    private ACommand command;
    private String message;
    private String key;
    private BufferedReader reader;
    private Logger logger;

    public CommandExtraThreadIOStream() {
        reader = new BufferedReader(new InputStreamReader(System.in));    
        logger = Logger.getLogger(CommandExtraThreadIOStream.class);
    }

    @Override
    public void initializeExtraThread(ACommand command, String msq, String key) {
        t = new Thread(this);
        this.command = command;
        this.key = key;
        this.message = msq;
        
    }

    @Override
    public Thread getThread() {
        return t;
    }
    

   
    @Override
    public void run() {
        try {
            System.out.print(message);
            logger.info(message);

            key = reader.readLine().intern();
            command.isValidKey(key);
            command.action(key);

            logger.info(key);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            logger.error(e.getMessage(), e);

            CommandExtraThreadIOStream c = new CommandExtraThreadIOStream();
            c.initializeExtraThread(command, "Are you sure? [Yes / No] : ", null);
            
        }

    }
}
